package com.dji.compose_learning.ui.view

import android.widget.Space
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.TextField
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.dp

val importantWords = listOf<String>("Compose", "Java", "buobao", "Study")

fun String.containsWords(list: List<String>): Boolean {
//    list.forEach {
//        if (this.contains(it)) {
//            return true
//        }
//    }
//    return false

    for (item in list) {
        if (this.contains(item)) return true
    }
    return false
}

@Composable
fun DerivedStateSample() {
    var inputText by remember {
        mutableStateOf("")
    }
    var addedList by remember {
        mutableStateOf(listOf<String>())
    }

    val highWords by remember(addedList, importantWords) {
        derivedStateOf {
            addedList.filter { it.containsWords(importantWords) }
        }
    }

    Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) {
        Column {
            Row(modifier = Modifier.fillMaxWidth()) {
                TextField(value = inputText, maxLines = 1, singleLine = true, onValueChange = {
                    inputText = it
                },
                    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
                    keyboardActions = KeyboardActions(onDone = {
                        addedList = addedList.toMutableList() + listOf(inputText)
                        inputText = ""
                    }, onNext = {})
                )
                Spacer(modifier = Modifier.width(10.dp))
                Button(onClick = {
                    addedList = addedList.toMutableList() + listOf(inputText)
                    inputText = ""
                }) {
                    Text(text = "Add", color = MaterialTheme.colors.onPrimary)
                }
            }
            Spacer(modifier = Modifier.height(10.dp))
//            LazyColumn(horizontalAlignment = Alignment.CenterHorizontally,
//                modifier = Modifier.fillMaxWidth()) {
//                items(highWords.size) {
//                    Text(text = addedList[it],
//                        color = MaterialTheme.colors.onBackground,
//                        modifier = Modifier.background(
//                            Color.LightGray))
//                }
//            }
            LazyColumn(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier.fillMaxWidth()) {
                items(highWords.size) {
                    Text(text = highWords[it],
                        color = MaterialTheme.colors.onBackground,
                        modifier = Modifier.background(
                            Color.LightGray))
                }

                items(addedList.size) {
                    Text(text = addedList[it], color = MaterialTheme.colors.onBackground)
                }
            }
        }
    }
}